﻿/*
 * glucose表的数据访问操作
 * */
using System;
using System.Collections.Generic;
using System.Text;
using WH.NurseSys.Model;
using System.Data.SqlClient;
using System.Data;

namespace WH.NurseSys.DAL
{
    public class GlucoseDAL
    {
        //插入一条新的血糖数据
        public bool Insert(Glucose glucose)
        {
            string sqlText = "insert into glucose (id,patient_id,value,measure_context,exec_time) values (@id,@patient_id,@value,@measure_context,@exec_time)";
            SqlParameter[] parameters = new SqlParameter[]{
                new SqlParameter("@id",glucose.Id),
                new SqlParameter("@patient_id",glucose.PatientId),
                new SqlParameter("@value",glucose.Value),
                new SqlParameter("@measure_context",glucose.MeasureContext),
                new SqlParameter("@exec_time",glucose.ExecTime)
            };
            int rows = SqlHelper.ExecuteNoQuery(sqlText, parameters);
            if (rows <= 0)
                return false;
            else
                return true;
        }

         //更新一条数据
        public bool Update(Glucose glucose)
        {
            string sqlText = "update glucose set patient_id=@patient_id,value=@value,measure_context=@measure_context,exec_time=@exec_time where id=@id";
            SqlParameter[] parameters = new SqlParameter[]{
                new SqlParameter("@id",glucose.Id),
                new SqlParameter("@patient_id",glucose.PatientId),
                new SqlParameter("@value",glucose.Value),
                new SqlParameter("@measure_context",glucose.MeasureContext),
                new SqlParameter("@exec_time",glucose.ExecTime)
            };
            int rows = SqlHelper.ExecuteNoQuery(sqlText, parameters);
            if (rows <= 0)
                return false;
            else
                return true;
        }

        //删除一条数据
        public bool Delete(Guid id)
        {
            string sqlText = "delete from glucose where id=@id";
            SqlParameter parameters = new SqlParameter("@id", id);
            int rows = SqlHelper.ExecuteNoQuery(sqlText, parameters);
            if (rows <= 0)
                return false;
            else
                return true;
        }

        //查询一条数据
        public Glucose GetById(Guid id)
        {
            string sqlText = "select * from glucose where id=@id";
            SqlParameter parameters = new SqlParameter("@id", id);
            DataTable dbTable = SqlHelper.ExecuteTable(sqlText, parameters);
            if (dbTable.Rows.Count <= 0)
                return null;
            DataRow dbRow = dbTable.Rows[0];
            Glucose glucose = new Glucose();
            glucose.Id = new Guid(dbRow["id"].ToString());
            glucose.PatientId = new Guid(dbRow["patient_id"].ToString());
            glucose.Value = Convert.ToSingle(dbRow["value"]);
            glucose.MeasureContext = Convert.ToInt32(dbRow["measure_context"]);
            glucose.ExecTime = Convert.ToDateTime(dbRow["exec_time"]);
            return glucose;
        }

        //获取一个人的数据列表
        public List<Glucose> GetListByPatient(Guid patientId)
        {
            List<Glucose> glucoseList = new List<Glucose>();
            string sqlText = "select * from glucose where patient_id=@patient_id order by exec_time asc";
            SqlParameter parameters = new SqlParameter("@patient_id", patientId);
            DataTable dbTable = SqlHelper.ExecuteTable(sqlText, parameters);
            foreach (DataRow dbRow in dbTable.Rows)
            {
                Glucose glucose = new Glucose();
                glucose.Id = (Guid)dbRow["id"];
                glucose.ExecTime = Convert.ToDateTime(dbRow["exec_time"]);
                glucose.MeasureContext = (int)dbRow["measure_context"];
                glucose.PatientId = patientId;
                glucose.Value = Convert.ToSingle(dbRow["value"]);
                glucoseList.Add(glucose);
            }
            return glucoseList;
        }
    }
}
